package com.mango.iot.gateway.coap.server.resource;

import com.mango.iot.gateway.coap.common.CoapResourceConst;
import com.mango.iot.gateway.coap.redis.RedisUtils;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.Option;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 基本数据token校验
 * </pre>
 *
 * @author  liangfeihu
 * @since  2023/6/28 16:01
 */
public class BaseCoapResource extends CoapResource {
    @Autowired
    protected RedisUtils ixCache;

    public BaseCoapResource(String name) {
        super(name);
    }

    protected String getToken(CoapExchange exchange) {
        String token = getTokenFromOption(exchange);
        if (!StringUtils.isEmpty(token)) {
            return token;
        } else {
            String uriQueryStr = exchange.getRequestOptions().getUriQueryString();
            Map<String, String> uriQueryMap = getUriQueryMap(uriQueryStr);
            return CollectionUtils.isEmpty(uriQueryMap) ? null : uriQueryMap.get("token");
        }
    }

    protected boolean validateToken(String productKey, String deviceNo, String token) {
        String key = productKey + deviceNo;
        Object value = ixCache.get(key);
        if (value == null) {
            return false;
        }
        if (!StringUtils.isEmpty(token) && token.equals(value.toString())) {
            return true;
        }
        return false;
    }

    protected String getParamValueFromUriQuery(CoapExchange exchange, String paramKey) {
        String uriQueryStr = exchange.getRequestOptions().getUriQueryString();
        Map<String, String> paramsMap = getUriQueryMap(uriQueryStr);
        return paramsMap.get(paramKey);
    }

    protected String getTokenFromOption(CoapExchange exchange) {
        List<Option> optionList = exchange.getRequestOptions().getOthers();
        if (!CollectionUtils.isEmpty(optionList)) {
            for (Option option : optionList) {
                if (CoapResourceConst.COAP2_OPTION_TOKEN == option.getNumber()) {
                    return option.getStringValue();
                }
            }
        }
        return null;
    }

    protected Map<String, String> getUriQueryMap(String uriQueryStr) {
        Map<String, String> map = new HashMap<>();
        if (!StringUtils.isEmpty(uriQueryStr)) {
            String[] queryItems = uriQueryStr.split("&");
            for (String queryItem : queryItems) {
                String[] item = queryItem.split("=");
                map.put(item[0], item[1]);
            }
        }
        return map;
    }

    protected Map<String, String> parseTopic(String topic) {
        Map<String, String> topicMap = new HashMap<>();
        String[] topics = topic.split("/");
        topicMap.put("productKey", topics[2]);
        topicMap.put("deviceNo", topics[3]);
        return topicMap;
    }

}
